#include<pthread.h>
#include"Thread_Impl.h"
Thread_Impl::Thread_Impl()
{
}

Thread_Impl::~Thread_Impl()
{
}

void * Thread_Impl::svc_run( void *apParam)
{
    ((Thread_Impl *)apParam)->svc();
}

int Thread_Impl::activate( int n_threads /*= 1*/ )
{
    pthread_t threadID;
    if(!h_threads.empty())
        return -1;

    while(n_threads)
    {
        if(pthread_create(&threadID, NULL, svc_run, (void*)this) == 0 )
            h_threads.push((unsigned long int)threadID);
        n_threads --;
    }
    return h_threads.size();
}

int	Thread_Impl::thr_count (void) const
{
    return h_threads.size();
}

void Thread_Impl::wait()
{
    std::queue<unsigned long int>  temp = h_threads;

    while(!temp.empty())
    {
        pthread_join((pthread_t)temp.front(),NULL);//  wait...
        temp.pop();
    }

    while(!h_threads.empty())
        h_threads.pop();
}
